package trans.model;

import java.util.Date;
import java.util.List;
import java.util.Locale;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.time.DateFormatUtils;

import trans.service.AttachmentDAO;
import trans.support.Constants;

public class OceanSa extends Sa{
	
	private static final long serialVersionUID = 1L;
	
	private java.lang.String portOfLoading;
	private java.lang.String placeOfDelivery;
	private java.lang.String vesselVoyageNo;
	private java.util.Date vesselDate;
	private java.lang.String qty;
	private java.lang.Integer paymentOptions;
	private java.lang.String blNo;
	private java.lang.String hbl;
	private java.lang.String forCargoRelease;
	
	private Date atdPol;
	private String etaPod;
	
	
	public Date getAtdPol() {
		return atdPol;
	}


	public void setAtdPol(Date atdPol) {
		this.atdPol = atdPol;
	}


	public String getEtaPod() {
		return etaPod;
	}


	public void setEtaPod(String etaPod) {
		this.etaPod = etaPod;
	}


	@Override
	public boolean caculatePendding() {
		return false;
	}


	public java.lang.String getPortOfLoading() {
		return portOfLoading;
	}
	
	@Override
	public String getStateShow() {
		return Constants.OCEANSA_STATES.get(this.getState());
	}


	@Override
	public boolean isFreeze(User invorkUser) {
		if(invorkUser.getUserRole() == Constants.ROLE_ADMIN) return false;
		if(this.getState() >= Constants.OCEANSA_STATUS_SAL){
			return true;
		}
		return false;
	}
	

	@Override
	public void prepareSaStateShow( HttpServletRequest request) {
		String flightDate = "<UNSET>";
		String vesselNo = "<UNSET>";
		if(this.getVesselDate() != null){
			 flightDate = DateFormatUtils.format(this.getVesselDate(), "MMM dd,yyyy", Locale.ENGLISH);
		}
		if(StringUtils.isNotEmpty(this.getVesselVoyageNo())){
			vesselNo = this.getVesselVoyageNo();
		}
		request.setAttribute("depTitle", "VESSEL NO.["+vesselNo +"]/ VESSEL DATE["+
				flightDate+"]");
	}
	
	@Override
	public  void initStates(User user,Map<Integer,String> usableStates) {
		for(Integer eachKey : Constants.OCEANSA_STATES.keySet()){
			usableStates.put(eachKey, Constants.OCEANSA_STATES.get(eachKey));
		}
	}
	
	@Override
	public void prepareTransNoShow(HttpServletRequest request) {
//		int state = this.getState();
		
		List<SaAttachment> attachments = 
			AttachmentDAO.getAllAttachments(this.getId());
		
		List<SaStateHis> hises = this.getStateHises();
		
		if(hisContain(hises,Constants.OCEANSA_STATUS_RBK)){
			request.setAttribute("status_rbk", "rbk.gif");
			if(fileContain(attachments,Constants.OCEANSA_STATUS_RBK)){
				request.setAttribute("rbk_attached", true);
			}
		}
		
		if(hisContain(hises,Constants.OCEANSA_STATUS_OWW)){
			request.setAttribute("status_oww", "oww.gif");
			if(fileContain(attachments,Constants.OCEANSA_STATUS_OWW)){
				request.setAttribute("oww_attached", true);
			}
		}
		
		
		if(hisContain(hises,Constants.OCEANSA_STATUS_RCF)){
			request.setAttribute("status_rcf", "rcf.gif");
			if(fileContain(attachments,Constants.OCEANSA_STATUS_RCF)){
				request.setAttribute("rcf_attached", true);
			}
		}
		
		if(hisContain(hises,Constants.OCEANSA_STATUS_RCD)){
			request.setAttribute("status_rcd", "rcd.gif");
			if(fileContain(attachments,Constants.OCEANSA_STATUS_RCD)){
				request.setAttribute("rcd_attached", true);
			}
		}
		
		if(hisContain(hises,Constants.OCEANSA_STATUS_SAH)){
			request.setAttribute("status_sah", "sah.gif");
			if(fileContain(attachments,Constants.OCEANSA_STATUS_SAH)){
				request.setAttribute("sah_attached", true);
			}
		}
		
		if(hisContain(hises,Constants.OCEANSA_STATUS_STP)){
			request.setAttribute("status_stp", "stp.gif");
			if(fileContain(attachments,Constants.OCEANSA_STATUS_STP)){
				request.setAttribute("stp_attached", true);
			}
		}
		
		if(hisContain(hises,Constants.OCEANSA_STATUS_WSR)){
			request.setAttribute("status_wsr", "wsr.gif");
			if(fileContain(attachments,Constants.OCEANSA_STATUS_WSR)){
				request.setAttribute("wsr_attached", true);
			}
		}
		
		if(hisContain(hises,Constants.OCEANSA_STATUS_SMP)){
			request.setAttribute("status_smp", "smp.gif");
			if(fileContain(attachments,Constants.OCEANSA_STATUS_SMP)){
				request.setAttribute("smp_attached", true);
			}
		}
		
		if(hisContain(hises,Constants.OCEANSA_STATUS_SAL)){
			request.setAttribute("status_sal", "sal.gif");
			if(fileContain(attachments,Constants.OCEANSA_STATUS_SAL)){
				request.setAttribute("sal_attached", true);
			}
		}
		
		if(hisContain(hises,Constants.OCEANSA_STATUS_DDV)){
			request.setAttribute("status_ddv", "ddv.gif");
			if(fileContain(attachments,Constants.OCEANSA_STATUS_DDV)){
				request.setAttribute("ddv_attached", true);
			}
		}
		
		if(hisContain(hises,Constants.OCEANSA_STATUS_ARR)){
			if(fileContain(attachments,Constants.OCEANSA_STATUS_ARR)){
				request.setAttribute("arr_attached", true);
			}
			request.setAttribute("status_arr", "arr.gif");
		}
		
		if(hisContain(hises,Constants.OCEANSA_STATUS_NFD)){
			if(fileContain(attachments,Constants.OCEANSA_STATUS_NFD)){
				request.setAttribute("nfd_attached", true);
			}
			request.setAttribute("status_nfd", "nfd.gif");
		}
		
		if(hisContain(hises,Constants.OCEANSA_STATUS_DLV)){
			if(fileContain(attachments,Constants.OCEANSA_STATUS_DLV)){
				request.setAttribute("dlv_attached", true);
			}
			request.setAttribute("status_dlv", "dlv.gif");
		}
	}
	
	private boolean hisContain(List<SaStateHis> hises, int oceansaStatusRbk) {
		if(hises == null){
			return false;
		}
		for(SaStateHis each : hises){
			if(each!=null &&each.getStateTo() == oceansaStatusRbk){
				return true;
			}
		}
		return false;
	}


	private  boolean fileContain(List<SaAttachment> attachments,Integer state){
		for(SaAttachment each : attachments){
			if(each.getRelativeStatus().equals(state)){
				return true;
			}
		}
		return false;
	}
	
	@Override
	public String getViewName() {
		return "oceanSaView";
	}
	
	@Override
	public String getEditName() {
		return "oceanSaEdit";
	}


	public void setPortOfLoading(java.lang.String portOfLoading) {
		this.portOfLoading = portOfLoading;
	}


	public java.lang.String getPlaceOfDelivery() {
		return placeOfDelivery;
	}


	public void setPlaceOfDelivery(java.lang.String placeOfDelivery) {
		this.placeOfDelivery = placeOfDelivery;
	}


	public java.lang.String getVesselVoyageNo() {
		return vesselVoyageNo;
	}


	public void setVesselVoyageNo(java.lang.String vesselVoyageNo) {
		this.vesselVoyageNo = vesselVoyageNo;
	}


	public java.util.Date getVesselDate() {
		return vesselDate;
	}


	public void setVesselDate(java.util.Date vesselDate) {
		this.vesselDate = vesselDate;
	}

	public java.lang.Integer getPaymentOptions() {
		return paymentOptions;
	}


	public void setPaymentOptions(java.lang.Integer paymentOptions) {
		this.paymentOptions = paymentOptions;
	}


	public java.lang.String getBlNo() {
		return blNo;
	}


	public void setBlNo(java.lang.String blNo) {
		this.blNo = blNo;
	}


	public java.lang.String getHbl() {
		return hbl;
	}


	public void setHbl(java.lang.String hbl) {
		this.hbl = hbl;
	}



	public java.lang.String getForCargoRelease() {
		return forCargoRelease;
	}


	public void setForCargoRelease(java.lang.String forCargoRelease) {
		this.forCargoRelease = forCargoRelease;
	}



	public java.lang.String getQty() {
		return qty;
	}


	public void setQty(java.lang.String qty) {
		this.qty = qty;
	}

	
}
